﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;

namespace WuLiu.Spider.BLL
{
    public class WebRequestLoginImpersonate
    {
        private string MakePostData(Dictionary<string, string> postData)
        {
            #region
            string postDataString = "";
            foreach (KeyValuePair<string, string> kvp in postData)
            {
                postDataString += kvp.Key + "=";
                if (kvp.Value == "")
                {
                    postDataString += "''";
                }
                else
                {
                    postDataString += kvp.Value;
                }
                postDataString += "&";
            }
            postDataString = postDataString.Substring(0, postDataString.Length - 1);
            return postDataString;
            #endregion
        }

        public CookieCollection GetCookie(Uri url, Dictionary<string, string> postData, RequestSendHeaderAttribute sendHeaderAttribute)
        {
            #region
            #region 设置请求信息
            var bPostData = Encoding.Default.GetBytes(MakePostData(postData));
            CookieContainer cookieContainer = new CookieContainer();
            HttpWebRequest hwRequest = (HttpWebRequest)WebRequest.Create(url);
            hwRequest.CookieContainer = cookieContainer;
            sendHeaderAttribute.Fill(hwRequest);
            hwRequest.ContentLength = bPostData.Length;
            #endregion

            #region 向请求流插入数据
            Stream requestStream = hwRequest.GetRequestStream();
            requestStream.Write(bPostData, 0, bPostData.Length);
            requestStream.Close();
            #endregion

            #region 获取返回流
            HttpWebResponse hwResponse;
            try
            {
                hwResponse = (HttpWebResponse)hwRequest.GetResponse();
                hwResponse.Cookies = cookieContainer.GetCookies(hwRequest.RequestUri);
            }
            catch
            {
                return new CookieCollection();
            }

            var resultHtml = "";
            StreamReader reader = new StreamReader(hwResponse.GetResponseStream());
            resultHtml = reader.ReadToEnd();
            reader.Close();
            #endregion

            var cookieCollection = hwResponse.Cookies;
            return cookieCollection;
            #endregion
        }

        public string GetHtmlContent(Uri url, CookieCollection cookieCollection, RequestSendHeaderAttribute sendHeaderAttribute)
        {
            #region
            #region 设置请求信息
            CookieContainer cookieContainer = new CookieContainer();
            cookieContainer.Add(cookieCollection);
            HttpWebRequest hwRequest = (HttpWebRequest)WebRequest.Create(url);
            hwRequest.CookieContainer = cookieContainer;
            sendHeaderAttribute.Fill(hwRequest);
            hwRequest.ContentLength = 0;
            var cookieString = "";
            foreach (Cookie item in cookieCollection)
            {
                cookieString += item.Name + ":" + item.Value + ";";
            }
            hwRequest.Headers.Add("Cookie",cookieString.TrimEnd(';'));
            #endregion

            #region 获取返回流
            HttpWebResponse hwResponse;
            try
            {
                hwResponse = (HttpWebResponse)hwRequest.GetResponse();
                hwResponse.Cookies = cookieContainer.GetCookies(hwRequest.RequestUri);
            }
            catch
            {
                return "";
            }

            var resultHtml = "";
            StreamReader reader = new StreamReader(hwResponse.GetResponseStream());
            resultHtml = reader.ReadToEnd();
            reader.Close();
            #endregion

            return resultHtml;
            #endregion
        }
    }
}
